package com.hermit.qmst.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hermit.qmst.R;
import com.hermit.qmst.mode.BusinessInfo;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.ImageDownLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/11 0011.
 */
public class BusinessAdapter extends BaseAdapter {
    private Context context;
    public List<BusinessInfo> data;
    private ImageDownLoader mImageDownLoader;

    public BusinessAdapter(Context context) {
        this.context = context;
        mImageDownLoader = new ImageDownLoader(context);
        data = new ArrayList<BusinessInfo>();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_business, null);

            holder.iv_divide = (ImageView) convertView.findViewById(R.id.iv_divide);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        holder.tv_title.setText(data.get(position).getTitle());
        holder.tv_time.setText(data.get(position).getAddress());
        String url = data.get(position).getImg();
        final ImageView imageView = holder.iv_divide;
        if (url.length() > 0) {
            ImageDownLoader imageDownLoader = new ImageDownLoader(context);
            Bitmap bitmap = imageDownLoader.downloadImage(Common.getDialImageDirPath(), url, new ImageDownLoader.onImageLoaderListener() {
                @Override
                public void onImageLoader(Bitmap bitmap, String url) {
                    Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
                    imageView.setBackgroundDrawable(drawable);
                }
            });
            if (bitmap != null) {
                imageView.setBackgroundDrawable(new BitmapDrawable(context.getResources(), bitmap));
            }
        }

        return convertView;
    }

    class Holder {
        ImageView iv_divide;
        TextView tv_title, tv_time;
    }
}
